/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fb.model;

import business.service.MongoDbFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.fb.common.Re_News_NewsCate_Data;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

/**
 *
 * @author Administrator
 */
public class Re_News_NewsCate_Model {
    private static final String Re_News_NewsCate_COLLECTION = "Re_News_NewsCate";
    private static final Logger logger = Logger.getLogger(Re_News_NewsCate_Model.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static Re_News_NewsCate_Model _instance = null;
    
    public static Re_News_NewsCate_Model getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new Re_News_NewsCate_Model();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }

    public Re_News_NewsCate_Model() {
        try {
        } catch (Exception ex) {
            logger.error("Re_News_NewsCate_Model: " + ex);
        }
    }
    
    
    public List<Re_News_NewsCate_Data> getItemsByNewsId(String idNews) {
        List<Re_News_NewsCate_Data> lstResult = null;
        try {                      
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(Re_News_NewsCate_COLLECTION);
            DBCursor cursor = coll.find(new BasicDBObject("idNews", idNews));            
            lstResult = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();                
                String objectId = ((ObjectId) dbObject.get("_id")).toString();
                String idNewsRe = (String) dbObject.get("idNews");
                String idNewsCateRe = (String) dbObject.get("idNewsCate");       
                long created = (long) dbObject.get("created");
                
                Re_News_NewsCate_Data newsData = new Re_News_NewsCate_Data(objectId, idNewsRe, idNewsCateRe, created);
                lstResult.add(newsData);
            }
        } catch (Exception ex) {
            logger.error("Re_News_NewsCate_Model.getItemsByNewsId: " + ex.getMessage(), ex);
        }
        return lstResult;
    }
    
    
     public String addItems(Re_News_NewsCate_Data re_News_NewsCate) {
        String id = "";
        try {
            BasicDBObject basicDBObject = new BasicDBObject()                    
                    .append("idNews", re_News_NewsCate.getIdNews())
                    .append("idNewsCate", re_News_NewsCate.getIdNewsCate())                    
                    .append("created", System.currentTimeMillis());
            
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(Re_News_NewsCate_COLLECTION);
            coll.insert(basicDBObject);
            
            ObjectId objectId = (ObjectId)basicDBObject.get( "_id" );
            id = objectId.toString();
        } catch (Exception ex) {
            logger.error("Re_News_NewsCate_Model.addItems: " + ex.getMessage(), ex);
        }
        return id;
    }
     
    public boolean deleteItemsByIdNewsCate(String id) {
        try {
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(Re_News_NewsCate_COLLECTION);
            coll.remove(new BasicDBObject("idNewsCate", id));

            return true;
        } catch (Exception ex) {
            logger.error("Re_News_NewsCate_Model.deleteItemsByIdNewsCate: " + ex.getMessage(), ex);
        }
        return false;
    }
    
    
    public boolean deleteItemsByIdNews(String id) {
        try {
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(Re_News_NewsCate_COLLECTION);
            coll.remove(new BasicDBObject("idNews", id));

            return true;
        } catch (Exception ex) {
            logger.error("Re_News_NewsCate_Model.deleteItemsByIdNews: " + ex.getMessage(), ex);
        }
        return false;
    }
}
